//
//  UpPhotoViewC.m
//  WHSport
//
//  Created by ccjoy-jimneylee on 15/5/27.
//  Copyright (c) 2015年 jimneylee. All rights reserved.
//

#import "UpPhotoViewC.h"

@interface UpPhotoViewC (){
    UIImage *photoImage;

}

@end

@implementation UpPhotoViewC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"上传照片";
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.row == 0) {
        UIActionSheet *sheet;
        sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self
                                   cancelButtonTitle:nil destructiveButtonTitle:@"取消"
                                   otherButtonTitles:@"拍照",@"从相册选择",
                 nil];
        [sheet showInView:self.view];
    }
    
    
}

- (IBAction)upBtn:(id)sender{
    
    if (photoImage == nil) {
        UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
                                                      message:@"请选择上传的图片"
                                                     delegate:self
                                            cancelButtonTitle:@"确定"
                                            otherButtonTitles:nil,nil];
        [alert show];
    }
    
    NSString *word = _textField.text;
    if (word == nil) {
        word = @"";
    }
    
//    NSArray *paths=NSSearchPathForDirectoriesInDomains
//    (NSDocumentDirectory,NSUserDomainMask, YES);
//    NSString *plistPaht=[paths objectAtIndex:0];
//    //取得完整的文件名
//    NSString *fileName=[plistPaht stringByAppendingPathComponent:@"Photo.plist"];
//    NSMutableArray *allArray = [NSMutableArray
//                                arrayWithContentsOfFile:fileName];
//    NSDictionary *dic =@{@"bdtext":word,@"bdimage":photoImage};
//    NSArray *array = [NSArray arrayWithObject:dic];
//    [allArray addObjectsFromArray:array];
//    //创建并写入文件
//    [allArray writeToFile:fileName atomically:YES];
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"Notification_UPHOTO" object:dic];
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - 保存图片至沙盒
- (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName
{
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
    // 获取沙盒目
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:imageName];
    // 将图片写入文件
    [imageData writeToFile:fullPath atomically:NO];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSUInteger sourceType = 0;
    if (buttonIndex == 0) {
        return ;
    }
    if (buttonIndex == 1) {
        sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    else{
        sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    }
    
    UIImagePickerController *imageController = [[UIImagePickerController alloc] init];
    imageController.delegate = self;
    imageController.allowsEditing = YES;
    imageController.sourceType = sourceType;
    [self presentViewController:imageController animated:YES completion:^{
        
    }];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    [picker dismissViewControllerAnimated:YES completion:^{
    }];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self saveImage:image withName:@"currentImage.png"];
    
    NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
                      stringByAppendingPathComponent:@"currentImage.png"];
    UIImage *save = [[UIImage alloc] initWithContentsOfFile:path];
    
    _photoImg.image = save;
    photoImage = save;
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}
/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
